package com.sxl.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sxl.config.ExcelUtil;
import com.sxl.config.LayUiResult;
import com.sxl.domain.*;
import com.sxl.service.NetfollowsService;
import com.sxl.service.StudentsService;
import com.sxl.service.UserrolesService;
import org.apache.commons.collections4.queue.UnmodifiableQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ProjectName: CRMProject
 * @Package: com.sxl.controller
 * @Author songxueliang
 * @Date 2022/5/2 15:04
 */
@RequestMapping("/netfollows")
@RestController
@CrossOrigin
public class NetfollowsController {
    @Autowired
    private NetfollowsService netfollowsService;
    @Autowired
    private UserrolesService userrolesService;
    @Autowired
    private StudentsService studentsService;
    /**
     * 全查跟踪信息表
     * @param netfollows
     * @return
     */
    @GetMapping("/selectAllNetfollows")
    public Map<String,Object> selectAllNetfollows(Netfollows netfollows){
        //查询登录的用户都有什么角色
        List<Userroles> userroles = userrolesService.selectUserrolesById(netfollows.getUserId());
        for (Userroles u : userroles) {
            if (u.getName().equals("咨询经理")) {
                netfollows.setUserId(null);
                break;
            } else {
                netfollows.setUserId(netfollows.getUserId());
            }
        }
        List<Netfollows> list = netfollowsService.selectAllByUserId(netfollows);
        int count = netfollowsService.getCount(netfollows);
        return LayUiResult.toClient("0","成功",count,list);
    }

    /**
     * 删除跟踪信息
     * @param id
     * @return
     */
    @PostMapping("deleteById")
    public Map<String,Object> deleteById(Integer id){
        boolean save = netfollowsService.removeById(id);
        return save==true?LayUiResult.toClient("0","删除成功"):LayUiResult.toClient("1","shanc失败");
    }

    /**
     * 修改跟踪信息
     * @param netfollows
     * @return
     */
    @PostMapping("/updateNetfollows")
    public Map<String,Object> updateNetfollows(Netfollows netfollows){
        UpdateWrapper<Students> wrapper = new UpdateWrapper();
        if (netfollows.getFollowState()==1){
            wrapper.set("stuStatus",4).set("isPay",0).eq("id",netfollows.getStudentId());
        }else if (netfollows.getFollowState()==2){
            wrapper.set("stuStatus",3).eq("id",netfollows.getStudentId());
        }else {
            wrapper.set("stuStatus",5).eq("id",netfollows.getStudentId());
        }
        studentsService.update(wrapper);
        boolean save = netfollowsService.updateById(netfollows);
        return save==true?LayUiResult.toClient("0","修改成功"):LayUiResult.toClient("1","修改失败");
    }

    /**
     * 添加跟踪信息
     * @param netfollows
     * @return
     */
    @PostMapping("/createNetfollows")
    public Map<String,Object> createNetfollows(Netfollows netfollows){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("studentId",netfollows.getStudentId());
        long count = netfollowsService.count(wrapper);
        UpdateWrapper<Students> wrapper1 = new UpdateWrapper();
        if (netfollows.getFollowState()==1){
            wrapper1.set("stuStatus",4).set("isPay",0).eq("id",netfollows.getStudentId());
        }else if (netfollows.getFollowState()==2){
            wrapper1.set("stuStatus",3).eq("id",netfollows.getStudentId());
        }else {
            wrapper1.set("stuStatus",5).eq("id",netfollows.getStudentId());
        }
        studentsService.update(wrapper1);
        if (count == 0){
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dqsj = df.format(new Date());
            UpdateWrapper<Students> updateWrapper = new UpdateWrapper();
            updateWrapper.set("firstVisitTime",dqsj).eq("id",netfollows.getStudentId());
            studentsService.update(updateWrapper);
        }
        boolean save = netfollowsService.save(netfollows);
        return save==true?LayUiResult.toClient("0","添加成功"):LayUiResult.toClient("1","添加失败");
    }
    /**
     * 导出excel
     *跟踪信息
     * @param response
     * @param netfollows
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response, Netfollows netfollows) {
        //查询登录的用户都有什么角色
        List<Userroles> userroles = userrolesService.selectUserrolesById(netfollows.getUserId());
        for (Userroles u : userroles) {
            if (u.getName().equals("咨询经理")) {
                netfollows.setUserId(null);
                break;
            } else {
                netfollows.setUserId(netfollows.getUserId());
            }
        }
        List<Netfollows_export> list = netfollowsService.export(netfollows);
        ExcelUtil.export(response, "students", "students", list, Netfollows_export.class);
    }
}
